﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;

namespace Admin.Finance
{
    public partial class MainInvoiceChargeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        #region gridView
        public void BindData()
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat(" and ContractType='0' and State='0'");//主包合同
            if (txtProjectNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and ProjectNumber like '%{0}%'", txtProjectNumber.Text.Trim());
            }
            if (txtProjectName.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and ProjectName like '%{0}%'", txtProjectName.Text.Trim());
            }
            if (txtContractNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and ContractNumber like '%{0}%'", txtContractNumber.Text.Trim());
            }
            if (txtContractName.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and ContractName like '%{0}%'", txtContractName.Text.Trim());
            }
            if (txtInvioceStartNumber.Text.Trim() != ""&&txtInvioceEndNumber.Text.Trim()!="")
            {
                strWhere.AppendFormat(" and InvoiceNumber between '{0}' and '{1}'", txtInvioceStartNumber.Text.Trim(),txtInvioceEndNumber.Text.Trim());
            }
            else if (txtInvioceStartNumber.Text.Trim() != "" && txtInvioceEndNumber.Text.Trim() == "")
            {
                strWhere.AppendFormat(" and InvoiceNumber>'{0}'", txtInvioceStartNumber.Text.Trim());
            }
            else if (txtInvioceStartNumber.Text.Trim() == "" && txtInvioceEndNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and InvoiceNumber<'{0}'", txtInvioceEndNumber.Text.Trim());
            }
            try
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                
                DataSet ds=bllBind.GetDataSet("SUM(InvoiceMoney) as SumInvoiceMoney,SUM(SumReceiveMoney) as SumReceiveMoneys", "V_FinaceMainInvoiceList", strWhere.ToString(),true);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    lblCount.Text = ds.Tables[0].Rows[0]["SumInvoiceMoney"].ToString();
                    lblPayCount.Text = ds.Tables[0].Rows[0]["SumReceiveMoneys"].ToString();
                    lblNotPayCount.Text = (Convert.ToDecimal(lblCount.Text) - Convert.ToDecimal(lblPayCount.Text)).ToString();
                }
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "V_FinaceMainInvoiceList", "MainInvoiceId", "*", strWhere.ToString(), "CreateDate desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
            }
        }
        #endregion

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }
    }
}